import { _decorator, Component, EventTarget } from 'cc';
import { BaseModule, IModule } from './base/BaseModule';
import { IReference } from './base/ReferencePool';

const { ccclass } = _decorator;

@ccclass('EventSystem')
export class EventSystem extends BaseModule  {
    clear(): void {
        throw new Error('Method not implemented.');
    }

    uid: string ;
    init(): void {

        this._eventTarget = new EventTarget();
    }
    shutdown(): void {
        this._eventTarget.clear();
        this._eventTarget = null;

    }
    
    private _eventTarget: any = null;


    /**
     * 监听事件
     * @param eventType 事件类型
     * @param callback 回调函数
     * @param target 目标对象
     */
    on(eventType: string, callback: (...args: any[]) => void, target?: object) {
        this._eventTarget.on(eventType, callback, target);
    }

    /**
     * 取消监听事件
     * @param eventType 事件类型
     * @param callback 回调函数（可选）
     * @param target 目标对象（可选）
     */
    off(eventType: string, callback?: (...args: any[]) => void, target?: object) {
        this._eventTarget.off(eventType, callback, target);
    }

    /**
     * 发送事件
     * @param eventType 事件类型
     * @param data 数据
     */
    emit(eventType: string, data?: any) {
        this._eventTarget.emit(eventType, data);
    }

    /**
     * 监听一次事件
     * @param eventType 事件类型
     * @param callback 回调函数
     * @param target 目标对象
     */
    once(eventType: string, callback: (...args: any[]) => void, target?: object) {
        this._eventTarget.once(eventType, callback, target);
    }
}